﻿using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Face8280WebSocketServer.DB.Log
{
    public class CustomEFLogger : ILogger
    {
        private ILogger<FaceDBContext> _logger;

        private string _CategoryName = null;
        public CustomEFLogger(string categoryName, ILogger<FaceDBContext> logger)
        {
            this._CategoryName = categoryName;
            _logger = logger;
        }

        public IDisposable BeginScope<TState>(TState state)
        {
            return null;
        }

        public bool IsEnabled(LogLevel logLevel)
        {
            return true;
        }

        public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
        {
            if (!_CategoryName.Equals("Microsoft.EntityFrameworkCore.Database.Command")) return;
            if (!eventId.Name.Equals("Microsoft.EntityFrameworkCore.Database.Command.CommandExecuting")) return;
            
            _logger.LogInformation(formatter.Invoke(state, exception));
        }
    }
}
